from datetime import datetime
from app.extensions import db
import logging

class Category(db.Model):
    __tablename__ = 'categories'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), unique=True, nullable=False)
    user_id = db.Column(db.BigInteger, db.ForeignKey('users.id'), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    articles = db.relationship('Article', backref='category_ref', lazy=True)
    
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'user_id': self.user_id,
            'create_time': self.create_time.isoformat(),
            'update_time': self.update_time.isoformat()
        }
        
    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
            return True
        except Exception as e:
            db.session.rollback()
            logging.error(f"保存分类失败: {str(e)}")
            return False
            
    def delete(self):
        try:
            db.session.delete(self)
            db.session.commit()
            return True
        except Exception as e:
            db.session.rollback()
            logging.error(f"删除分类失败: {str(e)}")
            return False

class Article(db.Model):
    __tablename__ = 'articles'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    title = db.Column(db.String(100), nullable=False)
    category_id = db.Column(db.BigInteger, db.ForeignKey('categories.id'), nullable=False)
    content = db.Column(db.Text, nullable=False)
    author_id = db.Column(db.BigInteger, db.ForeignKey('users.id'), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)

    likes = db.relationship('ArticleLike', backref='article', lazy=True)
    
    def to_dict(self):
        return {
            'id': self.id,
            'title': self.title,
            'category_id': self.category_id,
            'content': self.content,
            'author_id': self.author_id,
            'create_time': self.create_time.isoformat(),
            'update_time': self.update_time.isoformat()
        }
        
    def save(self):
        db.session.add(self)
        db.session.commit()
        
    def delete(self):
        db.session.delete(self)
        db.session.commit()

class ArticleLike(db.Model):
    __tablename__ = 'article_likes'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    article_id = db.Column(db.BigInteger, db.ForeignKey('articles.id'), nullable=False)
    user_id = db.Column(db.BigInteger, db.ForeignKey('users.id'), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)

    __table_args__ = (
        db.UniqueConstraint('article_id', 'user_id', name='uk_article_user'),
    )